package com.oa.android.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.oa.android.R;
import com.oa.android.entry.Gongdan;
import com.oa.android.utils.UConstants;
import com.oa.android.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zouqh on 2017/8/3.
 */

public class DetailsGongdActivity extends Activity implements View.OnClickListener{


    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    @InjectView(R.id.gdmc)
    TextView gdmc;
    @InjectView(R.id.gdzt)
    TextView gdzt;
    @InjectView(R.id.bmmc)
    TextView bmmc;
    @InjectView(R.id.clr)
    TextView clr;
    @InjectView(R.id.khxm)
    TextView khxm;
    @InjectView(R.id.khbh)
    TextView khbh;
    @InjectView(R.id.lxdh)
    TextView lxdh;
    @InjectView(R.id.khdz)
    TextView khdz;
    @InjectView(R.id.gdnr)
    TextView gdnr;
    @InjectView(R.id.clnr)
    TextView clnr;
    @InjectView(R.id.pdTime)
    TextView pdTime;
    @InjectView(R.id.startTime)
    TextView startTime;
    @InjectView(R.id.endTime)
    TextView endTime;
    @InjectView(R.id.hfTime)
    TextView hfTime;
    @InjectView(R.id.hfxm)
    TextView hfxm;
    @InjectView(R.id.hfjg)
    TextView hfjg;
    @InjectView(R.id.button1)
    TextView button1;
    @InjectView(R.id.button2)
    TextView button2;
    @InjectView(R.id.layout)
    LinearLayout layout;

    private Gongdan gongdan;
    public static DetailsGongdActivity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gongdan= (Gongdan) getIntent().getSerializableExtra("gd");
        setContentView(R.layout.gongd_des_activity);
        ButterKnife.inject(this);
        activity=this;
        titleText.setText("工单详情");
        gdmc.setText("工单类型名称："+gongdan.getLXMC());
        gdzt.setText("工单状态："+gongdan.getGDZTNAME());
        khxm.setText("客户姓名："+gongdan.getKHXM());
        khbh.setText("客户编号："+gongdan.getKHBM());
        bmmc.setText("部门名称："+gongdan.getDEPNAME());
//        clr.setText("处理人："+gongdan.getc);
        lxdh.setText("联系电话："+gongdan.getLXDH());
        khdz.setText("客户地址："+gongdan.getKHDZ());
        gdnr.setText(Html.fromHtml("工单内容："+gongdan.getGDNR()));
        pdTime.setText("派单时间："+gongdan.getPDSJ());
        if (null!=gongdan.getGDZT()){
            if (gongdan.getGDZT().equals("0")){
                button1.setText("接收工单");
            }else if (gongdan.getGDZT().equals("1")){
                button1.setText("处理工单");
                button2.setText("新建工作流");
                startTime.setText("处理开始时间："+gongdan.getCLKSSJ());
                startTime.setVisibility(View.VISIBLE);
                button2.setVisibility(View.VISIBLE);
            }else if (gongdan.getGDZT().equals("2")){
                button1.setText("回访工单");
                startTime.setText("处理开始时间："+gongdan.getCLKSSJ());
                startTime.setVisibility(View.VISIBLE);
                endTime.setText("处理结束时间："+gongdan.getCLJSSJ());
                endTime.setVisibility(View.VISIBLE);
                clnr.setText(  Html.fromHtml("处理内容："+gongdan.getCLNR()));
                clnr.setVisibility(View.VISIBLE);
            }else if (gongdan.getGDZT().equals("3")){
                layout.setVisibility(View.GONE);
                startTime.setText("处理开始时间："+gongdan.getCLKSSJ());
                startTime.setVisibility(View.VISIBLE);
                endTime.setText("处理结束时间："+gongdan.getCLJSSJ());
                endTime.setVisibility(View.VISIBLE);

                clnr.setText(  Html.fromHtml("处理内容："+gongdan.getCLNR()));
                clnr.setVisibility(View.VISIBLE);
                hfTime.setText("回访时间："+gongdan.getHFSJ());
                hfTime.setVisibility(View.VISIBLE);
                hfjg.setText("回访结果："+gongdan.getHFJG());
                hfjg.setVisibility(View.VISIBLE);
                hfxm.setText("回访姓名："+gongdan.getHFR());
                hfxm.setVisibility(View.VISIBLE);
            }
        }
        close.setOnClickListener(this);
        button2.setOnClickListener(this);
        button1.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                finish();
                break;
            case R.id.button1:
                if (gongdan.getGDZT().equals("0")){
                    getDate("1","");
                } else if (gongdan.getGDZT().equals("1")){
                    pointDialog("2","处理工单");
                }else if (gongdan.getGDZT().equals("2")){
                    pointDialog("3","回访工单");
                }
                break;
            case R.id.button2:
                Intent intent=new Intent(this,GDAddWorkActivity.class);
                intent.putExtra("id",gongdan.getDOID());
                startActivity(intent);
                break;
        }
    }

    ProgressDialog dialog;

    //接收工单
    private void getDate(String lx,String content) {
        dialog = ProgressDialog.show(this, null, "加载中...", true, false);
        RequestParams params = new RequestParams(UConstants.JS_GD);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("LX", lx+"");
        params.addBodyParameter("ID", gongdan.getID());
        params.addBodyParameter("DEPID", UConstants.DepId);
        if(lx.equals("2")){
            params.addBodyParameter("CLNR", content);
            params.addBodyParameter("DOID", "0");
        } else if(lx.equals("3")){
            params.addBodyParameter("HFNR", content);
            params.addBodyParameter("HFR", UConstants.EmpName);
        }
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);

                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        String success=  object.getString("success");
                        if (success.equals("true")){
                            if (gongdan.getGDZT().equals("0")){
                                Toast.makeText(DetailsGongdActivity.this,"接收成功",Toast.LENGTH_SHORT).show();
                                button1.setText("处理工单");
                                button2.setText("新建工作流");
                                gdzt.setText("处理中");
                                gongdan.setGDZT("1");
                                button2.setVisibility(View.VISIBLE);
                            } else if (gongdan.getGDZT().equals("1")){
                                button1.setText("回访工单");
                                gdzt.setText("已处理");
                                gongdan.setGDZT("2");
                                button2.setVisibility(View.GONE);
                                Toast.makeText(DetailsGongdActivity.this,"处理成功",Toast.LENGTH_SHORT).show();
                            }else if (gongdan.getGDZT().equals("2")){
                                gongdan.setGDZT("3");
                                gdzt.setText("已回访");
                                layout.setVisibility(View.GONE);
                                Toast.makeText(DetailsGongdActivity.this,"回访成功",Toast.LENGTH_SHORT).show();
                            }
                            GongdActivity.activity.setRef();
                        }else{
                            Toast.makeText(DetailsGongdActivity.this,object.getString("msg")+"",Toast.LENGTH_SHORT).show();
                        }
//
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(DetailsGongdActivity.this,"操作失败",Toast.LENGTH_SHORT).show();
                    }
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Dialog Jdialog;
    /**
     * 提示
     */
    private void pointDialog(final String str,String title) {
        LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.exti_dialog_x,
                null);
        // 对话框
        Jdialog = new Dialog(this);
        Jdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Jdialog.show();

        Jdialog.setContentView(layout);
        TextView sure = (TextView) layout.findViewById(R.id.sureExti);
        TextView cancle = (TextView) layout.findViewById(R.id.cancleExti);
        TextView dialogTitle = (TextView) layout.findViewById(R.id.dialogTitle);
        dialogTitle.setText(title);
        final EditText text = (EditText) layout.findViewById(R.id.text);
        text.setHint("请输入内容");
//        dialogTitle.setText(str);
        sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//				ContextUtil.getInstance().finishApp();
                getDate(str,text.getText().toString());

                Jdialog.cancel();


            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Jdialog.cancel();
            }
        });

    }
}
